import { getToken, setToken, removeToken } from '@/utils/auth'
import { getProfile, getUserDetailById } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    updateToken(state, token) {
      state.token = token
      setToken(token)
    },
    delToken(state) {
      state.token = ''
      removeToken()
    },
    updateUserInfo(state, info) {
      state.userInfo = info
    },
    removeUserInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async updateUserInfo(context) {
      try {
        const res = await getProfile()
        const info = await getUserDetailById(res.data.userId)
        context.commit('updateUserInfo', { ...res.data, ...info.data })
        console.log(res)
      } catch (error) {
        // error
      }
    }
  }
}

